import greenfoot.*;  
import javax.swing.*;
import java.util.*;

/**
 * Player actor used by the SandBags game
 * 
  * @author Jasper Jans
 */
public class Player extends Actor
{
    
        private MiniGame miniGame;
    
        // actor images
        private GreenfootImage[] forwardImages = new GreenfootImage[10];
        private GreenfootImage[] downImages = new GreenfootImage[10];
        private GreenfootImage[] rightImages = new GreenfootImage[10];
        private GreenfootImage[] leftImages = new GreenfootImage[10];
    
   
        private int speed = 5;  // current player speed
        private int frame;      // current sequence image of walking player
        
        private boolean walking;  
        private boolean isKeyPressed; 
        private boolean facingUp;
        private boolean facingRight;     
        private boolean facingLeft;
        private boolean facingDown;
        private boolean poweringup = false;  // player is holding spacebar, throw is powering up
        
        private Sandbag sandBag;
        
        private Direction direction = Direction.RIGHT;  // current player direction

        private double power = 0.0;
        
        public void setSpeed(int speed)
        {
            this.speed = speed;
        }
        
        
        public void setSandbag(Sandbag sandbag)
        {
            this.sandBag = sandbag;
        }
        
        public Sandbag getSandbag()
        {
            return this.sandBag;
        }
        
        public Player(MiniGame miniGame)  
        {  
            this.miniGame = miniGame;
            
            loadPlayerImages(leftImages, "left");
            loadPlayerImages(forwardImages, "forward");
            loadPlayerImages(rightImages, "right");
            loadPlayerImages(downImages, "down");
            
            setImage(leftImages[1]);  
            walking = false;  
    
        }    

        /*
         * Loads all images for the given direction
         */
        private void loadPlayerImages(GreenfootImage[] imageList, String direction)
        {
            for(int i = 1; i <= 10; i++)
            {
                imageList[i-1] = new GreenfootImage(direction + Integer.toString(i) + ".png");
            }
        }
  
       
    public void act()   
    {  
        if(!miniGame.getGameStatus().equals(GameStatus.PLAYING)) return;    // Disallow movement when game is not playing

        speed = getObjectsInRange(25, Water.class).size() == 0 ? 5 : 2; // slow down when I am walking in the water
        
        checkKeys(); 
           
        if(sandBag != null)
        {
            if(facingUp)
            {
                sandBag.setLocation(getX(), getY() - 30);
            }
            else if(facingDown)
            {
                sandBag.setLocation(getX(), getY() + 30);
            }else if(facingLeft)
            {
                sandBag.setLocation(getX() - 30, getY());
            }
            else
            {
                sandBag.setLocation(getX() + 30, getY());
            }
        }
       
    }  
    
    /*
     * Checks to see if the player can move to the given coordinates
     */    
    private boolean canMove(int x, int y)
    {
        boolean canMove = true;
        if(Greenfoot.isKeyDown("up") && y <= 60) return false;
        
        List breach = getWorld().getObjectsAt(x, y, Breach.class);
        
        if(!breach.isEmpty()) return false;
        
        return true;
    }
      
    public void checkKeys()  
    {  
            isKeyPressed = false;  
            
            int x, y;
            
            x = getX();
            y = getY();
            
            if (Greenfoot.isKeyDown("up") && Greenfoot.isKeyDown("down"))  
            {  
                stopWalking();  
                isKeyPressed = true;                          
            }  
            else if(Greenfoot.isKeyDown("up") && Greenfoot.isKeyDown("right"))
            {
                walkRight();
                x+=speed;
                y-=speed;
                
                isKeyPressed = true;
            }
            else if(Greenfoot.isKeyDown("up") && Greenfoot.isKeyDown("left"))
            {
                walkLeft();
                x-=speed;
                y-=speed;

                isKeyPressed = true;
            }
            else if(Greenfoot.isKeyDown("down") && Greenfoot.isKeyDown("left"))
            {
                walkLeft();
                x-=speed;
                y+=speed;

                isKeyPressed = true;
            }
            else if(Greenfoot.isKeyDown("down") && Greenfoot.isKeyDown("right"))
            {
                walkRight();
                x+=speed;
                y+=speed;

                isKeyPressed = true;
            }
            else if (Greenfoot.isKeyDown("up"))  
            {  
                walkUp();  
               
                y-=speed;

                isKeyPressed = true;  
            }  
            else if (Greenfoot.isKeyDown("down"))  
            {  
                walkDown();  
 
                y += speed;
        
                isKeyPressed = true;  
            }  
            
            else if (Greenfoot.isKeyDown("right"))  
            {  
                walkRight();  
                x += speed;

                isKeyPressed = true;  
            }  
            else if (Greenfoot.isKeyDown("left"))  
            {  
                walkLeft();  
                x -= speed;
            
                isKeyPressed = true;  
            }
            else if(sandBag != null && direction != Direction.TOP && direction != Direction.DOWN && Greenfoot.isKeyDown("space"))
            {
                poweringup = true;
                
                if(power < 10)
                {
                    power = power + 1;
                }else{
                    throwSandBag();
                }
                
                
            }
            else if(sandBag != null && poweringup && !Greenfoot.isKeyDown("space"))
            {
                throwSandBag();
            }
            if (!(isKeyPressed))  
            {  
                stopWalking();  
            }  
            
            if(canMove(x, y))
            {
                setLocation(x,y);
            }
            
  
        }  
      
        /*
         * Throws the sandbag if available. 
         */
        private void throwSandBag()
        {
            if(sandBag != null)
            {
                sandBag.throwMe(power, direction);     
                setSandbag(null);
                power = 0.0;
                poweringup = false;
            }
        }
        

        
        public void walkUp()  
        {  
            
            direction = Direction.TOP;
            
            walking = true;  
            facingUp = true; 
            facingDown = false;
            facingLeft = false;
            facingRight = false;
            
            frame ++;  
            
            setImage(forwardImages[frame]);
            
            if(frame == 9) 
                frame = 0;
          
        }  
      public void walkDown()  
        {  
            direction = Direction.DOWN;
            walking = true;  
            facingDown = true;  
            facingUp = false;
            facingLeft = false;
            facingRight = false;
            
            frame ++;  
            
            setImage(downImages[frame]);
    
            if(frame==9) 
                frame = 0;
        }  
        public void walkLeft()  
        {  
            direction = Direction.LEFT;
            
            walking = true;  
            facingLeft = true;  
            facingUp = false;
            facingDown = false;
            facingRight = false;
            
            frame ++;  
            
            setImage(leftImages[frame]);
            
            if(frame == 9) 
                frame = 0;
          
        }  
        public void walkRight()  
        {  
            direction = Direction.RIGHT;
            walking = true;  
            facingRight = true;  
            facingUp = false;
            facingDown = false;
            facingLeft = false;
            
            frame ++;  
            
            setImage(rightImages[frame]);
            
            if(frame == 9) 
                frame = 0;
          
        }  
      
        public void stopWalking()  
        {  
            while( walking = false)  {
            if (facingUp = true){
                setImage (forwardImages[1]); 
            }       
            if(facingLeft = true){
                setImage (leftImages[1]);
            }
            if(facingDown = true){
                setImage (downImages[1]);
            }
            if(facingRight = true){
                setImage (rightImages[1]);
            }
                
        }
    }   
}
